package com.example.demo.Task;

import com.example.demo.service.EnableScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

/**
* @Description: 定时任务调度
* @Author: wuhs
* @Date: 2024/5/7
*/
@Component
@EnableScheduling
public class TaskSchedule implements SchedulingConfigurer {
    @Autowired
    private EnableScheduleService enableScheduleService;

    private String cron = "*/1 * * * * ?";//1秒执行一次
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.addTriggerTask(this::run, triggerContext ->
                new CronTrigger(cron).nextExecutionTime(triggerContext));
    }

    private void run(){
        enableScheduleService.enableSchedule();
    }
}
